<?php

include_once F_SOURCE . 'launch/launch.model.class.php';
include_once F_SOURCE . 'ads/ads.model.class.php';

/**
 * Keywords 投放管理
 * 
 * @author Dean
 *
 */
class kwsCtrl extends Ctrl{
	
	public function show(){
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		
		$params = $this->getShowParams();
		
		$adsModel = new adsModel();
		$number = $adsModel->countAllByType("keywords", $params);
		$adsList = $adsModel->findAll(($pageIndex-1)*PageNum, PageNum, 'keywords', $params);
		
		foreach ($adsList as $key => $value) {
			$info = $this->getInfoFromImportLaunch($value->id);
			if(!isset($info->order)){
				unset($adsList[$key]);				
			}else{
				$adsList[$key]->order = $info->order;
				$adsList[$key]->activity = $info->activity;
			}
		}
		
		$page = pageCount($number, $pageIndex, '?do=kws.show');
		
		$host = include F_CONF . 'host.'.Env_Config.'.config.php';
		
		$data = array(
			'op_select' => 'kws',
			'child_select' => 'show',
			'adsList' => $adsList,
			'page' => $page,
			'params'=>$params,
			'cloud_host' => $host,
		);
		
		$view->display($data, 'kw-list.html');
	}
	
	public function show_l(){
		$view = new View();
	
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
	
		$params = $this->getShowParams();
	
		$launchModel = new launchModel();
	
		$number = $launchModel->countAllByType("keywords", $params);
		$launchList = $launchModel->findAllByType("keywords", ($pageIndex-1)*PageNum, PageNum, $params);
	
		$page = pageCount($number, $pageIndex, '?do=kws.show_l');
	
		foreach ($launchList as $key => $value) {
			if(checkVaildDate($value->start, $value->end)===true){
				$launchList[$key]->status = "有效（执行中）";
			}
		}
		
		$host = include F_CONF . 'host.'.Env_Config.'.config.php';
		
		$data = array(
			'op_select' => 'kws',
			'child_select' => 'show_l',
			'launchList' => $launchList,
			'page' => $page,
			'params'=>$params,
			'cloud_host' => $host,
		);
	
		$view->display($data, 'kw-list-l.html');
	}
	
	public function view(){
		$view = new View();
		
		$host = include F_CONF . 'host.'.Env_Config.'.config.php';
		
		$data = array(
				'op_select' => 'kws',
				'child_select' => 'info',
				'op' => 'add_self',
				'isHDID' => true,
				'cloud_host' => $host,
		);
		$view->display($data, 'kw-insert.html');
	}
	
	public function info(){
	
		$view = new View();
	
		$id = $_GET['id'];
	
		$launchModel = new launchModel();
		$launchInfo = $launchModel->findOne($id);
	
		$imgInfo = $launchModel->findImgByID($id);
		
		$isHDID = isHDID($id);
		
		$host = include F_CONF . 'host.'.Env_Config.'.config.php';
		
		$data = array(
				'op' => 'update',
				'child_select' => 'show',
				'op_select' => 'kws',
				'obj' => $launchInfo,
				'imgInfo' => $imgInfo,
				'isHDID' => $isHDID,
				'cloud_host' => $host,
		);
		
		if(isset($imgInfo->id)){
			$data['img']=$imgInfo->img;
		}
	
		$view->display($data, 'kw-insert.html');
	}
	
	public function infoadd(){
	
		$view = new View();
	
		$id = $_GET['id'];
		$a = getGetStringParam('a');
		
		if($a=='import'){
			$launchModel = new launchModel();
			$adsInfo = $launchModel->import_findOne($id);
		}else{
			$adsModel = new adsModel();
			$adsInfo = $adsModel->findOne($id);
			
			$info = $this->getInfoFromImportLaunch($id);
			$adsInfo->order = $info->order;
			$adsInfo->activity = $info->activity;
		}
		
		$isHDID = isHDID($id);
	
		$host = include F_CONF . 'host.'.Env_Config.'.config.php';
		
		$data = array(
				'op' => 'add',
				'op_select' => 'kws',
				'child_select' => 'info',
				'obj' => $adsInfo,
				'isHDID' => $isHDID,
				'cloud_host' => $host,
		);
		
		if($a=='import'){
			$data['a'] = 'import';
		}
	
		$view->display($data, 'kw-insert.html');
	}
	
	/**
	 * 新增KWS投放操作
	 */
	public function add(){
	
		$adid = getPostStringParam('adid');
		$name =  getPostStringParam('name');
		$launchname =  getPostStringParam('launchname');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$size = getPostStringParam('size');
		$keywords = getPostStringParam('keywords');
		$num = getPostIntParam('num');
		$a = getPostIntParam('a');
		$order = getPostStringParam('order');
		$activity = getPostStringParam('activity');
		$type = 'keywords';
	
		$newID = '';
		$info = $this->getInfoFromImportLaunch($adid);
		if(!empty($info) && $info->id!=''){
			$newID = $info->id;
		}else{
			go('index.php?do=kws.info&msg=error_miss_launchinfo&id='.$adid);
		}
		
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
	
		$data = array(
			'launch_name' => $launchname,
			'name' => $name,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'size' => $size,
			'keywords' => $keywords,
			'createTime' => $time,
			'modifyTime' => $time,
			'createUser' => $createUser,
			'ads_id' => $adid,
			'num' => $num,
			'type' => $type,
			'id' => $newID,
			'order' => $order,
			'activity' => $activity
		);
	
		$launchModel = new launchModel();
		$insertID = $launchModel->add($data);
			
		if($insertID >= 0){
//			if($a=='import'){
//				$launchModel->import_modify(array('op'=>'c'), array('id'=>$adid));
//			}else{
				$adsModel = new adsModel();
				$adsModel->modify(array('op'=>'c'), array('id'=>$adid));
//			}
				
			go('index.php?do=kws.show_l&msg=success&id='.$adid);
		}else{
			go('index.php?do=kws.infoadd&msg=error&id='.$adid);
		}
	}
	
	public function add_self(){
	
		$name =  getPostStringParam('name');
		$launchname =  getPostStringParam('launchname');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$size = getPostStringParam('size');
		$keywords = getPostStringParam('keywords');
		$num = getPostIntParam('num');
		$a = getPostIntParam('a');
		$order = getPostStringParam('order');
		$activity = getPostStringParam('activity');
		$type = 'keywords';
		$tpl = getPostIntParam('tpl');
	
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
	
		$id = $this->getNextID();
		
		$data = array(
			'id' => $id,
			'launch_name' => $launchname,
			'name' => $name,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'size' => $size,
			'keywords' => $keywords,
			'createTime' => $time,
			'modifyTime' => $time,
			'createUser' => $createUser,
			'ads_id' => $adid,
			'num' => $num,
			'type' => $type,
			'order' => $order,
			'activity' => $activity,
			'tpl' => $tpl
		);
	
		$launchModel = new launchModel();
		$insertID = $launchModel->add($data);
			
		if($insertID >= 0){
			
			// add img for launch
			$img = getPostStringParam('img');
			if(!empty($img) && $img!=''){
				$img_title = getPostStringParam('img_title');
				$img_desc = getPostStringParam('img_desc');
				$img_outurl = getPostStringParam('img_outurl');
				$img_price = getPostStringParam('img_price');
				$imgArr  = array(
					'img' => $img,
					'img_title' => $img_title,
					'img_desc' => $img_desc,
					'img_outurl' => $img_outurl,
					'img_price' => $img_price,		
				);
				$launchModel->addImg($id, $imgArr);
			}
	
			go('index.php?do=kws.show_l&msg=success&id='.$insertID);
		}else{
			go('index.php?do=kws.info&msg=error');
		}
	}
	
	public function modify(){
	
		$id = getPostStringParam('id');
		$name =  getPostStringParam('name');
		$launchname =  getPostStringParam('launchname');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$size = getPostStringParam('size');
		$keywords = getPostStringParam('keywords');
		$num = getPostIntParam('num');
		$order = getPostStringParam('order');
		$activity = getPostStringParam('activity');
		$tpl = getPostIntParam('tpl');
	
		$time = getNowTime();
	
		$data = array(
			'launch_name' => $launchname,
			'name' => $name,
			'start' => $start,
			'end' => $end,
			'weight' => $weight,
			'click_url' => $clickurl,
			'target_window' => $window,
			'status' => $status,
			'size' => $size,
			'keywords' => $keywords,
			'modifyTime' => $time,
			'num' => $num,
			'order' => $order,
			'activity' => $activity,
			'tpl' => $tpl
		);
	
		$where = array(
				'id' => $id
		);
	
		$launchModel = new launchModel();
		$number = $launchModel->modify($data, $where);
			
		$view = new View();
		
		if($number > 0){
			
			// modify img for launch
			$img = getPostStringParam('img');
			if(!empty($img) && $img!=''){
				$img_title = getPostStringParam('img_title');
				$img_desc = getPostStringParam('img_desc');
				$img_outurl = getPostStringParam('img_outurl');
				$img_price = getPostStringParam('img_price');
				$imgArr  = array(
					'img' => $img,
					'img_title' => $img_title,
					'img_desc' => $img_desc,
					'img_outurl' => $img_outurl,
					'img_price' => $img_price,		
				);
				$launchModel->updateImg($id, $imgArr);
			}
			
			go('index.php?do=kws.show_l&msg=success&id='.$id);
		}else{
			go('index.php?do=kws.info&msg=error&id='.$id);
		}
	}
	
	public function delete(){
		
		$id = $_GET['id'];
		$type = $_GET['t'];
		
		$allowArr = array('us','ads','import');
		if(!in_array($type, $allowArr)){
			go('index.php?do=kws.show&msg=error&id='.$id);
		}
		
		if($type=='us'){
			$launchModel = new launchModel();
			$launchModel->delete(array('id'=>$id));		

			go('index.php?do=kws.show_l&msg=success');
		}elseif ($type=='ads'){
			$adsModel = new adsModel();
			$adsModel->delete(array('id'=>$id));
		}
		go('index.php?do=kws.show&msg=success');
		
	}
	
	public function showimg(){
		
		$id = getPostStringParam('id');
		
		$launchModel = new launchModel();
		$imgInfo = $launchModel->findImgByID($id);
		
		echo $imgInfo->img;
	}
	
	public function preview(){
		$img = getGetStringParam('img');
		$img_title = getGetStringParam('img_title');
		$img_desc = getGetStringParam('img_desc');
		$img_price = getGetStringParam('img_price');
		$img_outurl = getGetStringParam('img_outurl');
		$tpl = getGetIntParam('tpl');
		
		$img_id = getGetStringParam('id');
		if(!empty($img_id) && $img_id!=''){
			$launchModel = new launchModel();
			$imgInfo = $launchModel->getKWImgByID($img_id);
			
			$img = $imgInfo->img;
			$img_title = $imgInfo->img_title;
			$img_desc = $imgInfo->img_desc;
			$img_price = $imgInfo->img_price;
			$img_outurl = $imgInfo->img_outurl;
			$tpl = $imgInfo->tpl;
		}
		
		$action_host = "http://".$_SERVER['HTTP_HOST']."/upload_file/image/";
		
		$htmlContent = file_get_contents(F_ROOT.'template/kw_show_mockup_'.$tpl.'.html');
		
		$htmlContent = str_replace("IMG_HOLDER", $action_host.$img, $htmlContent);
		$htmlContent = str_replace("TITLE_HOLDER", $img_title, $htmlContent);
		$htmlContent = str_replace("DESC_HOLDER", $img_desc, $htmlContent);
		$htmlContent = str_replace("PRICE_HOLDER", $img_price, $htmlContent);
		
		if(stripos($img_outurl, "http://")===false){
			$img_outurl = "http://".$img_outurl;
		}
		$htmlContent = str_replace("OUT_HOLDER", $img_outurl, $htmlContent);
		
		$outHtmlContent = file_get_contents(F_ROOT.'template/kw_show_preview.html');
		$outHtmlContent = str_replace("OUT_CONTENT_HOLDER", $htmlContent, $outHtmlContent);
		
		echo $outHtmlContent;
		exit;
	}
	
	private function getShowParams(){
		$data = array();
		
		$data['v_id'] = getPostStringParam('v_id');
		$data['v_order'] = getPostStringParam('v_order');
		$data['v_activity'] = getPostStringParam('v_activity');
		$data['v_name'] = getPostStringParam('v_name');
		$data['v_launch_name'] = getPostStringParam('v_launch_name');
		$data['v_start'] = getPostStringParam('v_start');
		$data['v_end'] = getPostStringParam('v_end');
		$data['v_weight'] = getPostStringParam('v_weight');
		$data['v_keywords'] = getPostStringParam('v_keywords');
		$data['v_num'] = getPostStringParam('v_num');
		
		return $data;
	}
	
	private function getInfoFromImportLaunch($id){
		$adsModel = new adsModel();
		$launchInfo = $adsModel->getImportInfoByAdsID($id);
		
		return $launchInfo;
	}
	
	private function getNextID(){
		$launchModel = new launchModel();
		$kwInfo = $launchModel->getMaxID();
	
		$nextID = 'HD100001';
	
		if(isset($kwInfo->id)){
			$maxnum = intval(str_replace('HD', '', $kwInfo->id));
			$nextID = 'HD'.($maxnum+1);
		}
		return $nextID;
	}
}